package org.ewicom.ppsaddressbook.model;

import java.net.MalformedURLException;
import java.net.URL;

import org.ewicom.ppsaddressbook.PPSAddressBook.PPSAddressBookPreferences;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class ReleaseInfo implements Parcelable {

	private int id;
	private String releaseDate;
	private int versionNumber;
	private String versionName;
	private String symbol;
	private URL apkUrl;
	private URL changelogUrl;
	private String changelog;
	
	public ReleaseInfo(){
	}
	
	public ReleaseInfo(JSONObject jsonObject, String changelog){
		try {
			setId(jsonObject.getInt(PPSAddressBookPreferences.RELEASE_JSON_NAME_ID));
			
			setReleaseDate(jsonObject.getString(PPSAddressBookPreferences.RELEASE_JSON_NAME_RELEASE_DATE));
			
			setVersionNumber(jsonObject.getInt(PPSAddressBookPreferences.RELEASE_JSON_NAME_VERSION_NUMBER));
			setVersionName(jsonObject.getString(PPSAddressBookPreferences.RELEASE_JSON_NAME_VERSION_NAME));
			setSymbol(jsonObject.getString(PPSAddressBookPreferences.RELEASE_JSON_NAME_SYMBOL));
			
			setApkUrl(new URL(jsonObject.getString(PPSAddressBookPreferences.RELEASE_JSON_NAME_APK_URL)));
			setChangelogUrl(new URL(jsonObject.getString(PPSAddressBookPreferences.RELEASE_JSON_NAME_CHANGELOG_URL)));
			
			setChangelog(changelog);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ReleaseInfo(Parcel in){
		try {
			setId(in.readInt());
			
			setReleaseDate(in.readString());
			
			setVersionNumber(in.readInt());
			setVersionName(in.readString());
			setSymbol(in.readString());
			
			setApkUrl(new URL(in.readString()));
			setChangelogUrl(new URL(in.readString()));
			
			setChangelog(in.readString());
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static final Parcelable.Creator<ReleaseInfo> CREATOR = new Parcelable.Creator<ReleaseInfo>() {

		@Override
		public ReleaseInfo createFromParcel(Parcel source) {
			return new ReleaseInfo(source);
		}

		@Override
		public ReleaseInfo[] newArray(int size) {
			return new ReleaseInfo[size];
		}
	};
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(getId());
		dest.writeString(getReleaseDate());
		dest.writeInt(getVersionNumber());
		dest.writeString(getVersionName());
		dest.writeString(getSymbol());
		dest.writeString(getApkUrl().toString());
		dest.writeString(getChangelogUrl().toString());
		dest.writeString(getChangelog());
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(String releaseDate) {
		this.releaseDate = releaseDate;
	}

	public int getVersionNumber() {
		return versionNumber;
	}

	public void setVersionNumber(int versionNumber) {
		this.versionNumber = versionNumber;
	}

	public String getVersionName() {
		return versionName;
	}

	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}

	public String getSymbol() {
		return symbol;
	}

	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}

	public URL getApkUrl() {
		return apkUrl;
	}

	public void setApkUrl(URL apkUrl) {
		this.apkUrl = apkUrl;
	}

	public URL getChangelogUrl() {
		return changelogUrl;
	}

	public void setChangelogUrl(URL changelogUrl) {
		this.changelogUrl = changelogUrl;
	}

	public String getChangelog() {
		return changelog;
	}

	public void setChangelog(String changelog) {
		this.changelog = changelog;
	}
}
